<template>
  <div class="wb-main">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @selection-change="selectionChange"
      @on-load="getDataList"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click.stop="addOrUpdateHandle()"
        >新增</el-button>

        <el-button
          type="danger"
          size="small"
          :disabled="dataListSelections.length <= 0"
          @click="deleteHandle()"
        >批量删除</el-button>
      </template>

      <template
        slot="status"
        slot-scope="scope"
      >
        <el-tag
          v-if="scope.row.status === 1"
          size="small"
        >上架</el-tag>
        <el-tag
          v-else
          size="small"
        >未上架</el-tag>
      </template>

      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="small"
          @click="addOrUpdateHandle(scope.row.id)"
        >修改</el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          @click="deleteHandle(scope.row.id)"
        >删除</el-button>

      </template>
    </avue-crud>

  </div>
</template>

<script>
import { tableOption } from '@/crud/prod/prodList'
import prodApi from '@/api/prod'

export default {
  components: {

  },
  data() {
    return {
      dataForm: {
        prodName: ''
      },
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataListSelections: [],
      dataListLoading: false,
      tableOption: tableOption

    }
  },
  methods: {
    // 获取数据列表
    getDataList(page, params, done) {
      this.dataListLoading = true
      prodApi.prodPage(Object.assign(
        {
          pageNum: page == null ? this.page.currentPage : page.currentPage,
          pageSize: page == null ? this.page.pageSize : page.pageSize
        },
        params
      )).then(({ data }) => {
        this.dataList = data.records
        for (const key in this.dataList) {
          if (this.dataList.hasOwnProperty(key)) {
            const element = this.dataList[key]
            element.imgs = process.env.VUE_APP_BASE_API + element.imgs.split(',')[0]
            element.pic = process.env.VUE_APP_BASE_API + element.pic
            element.status = element.status ? 1 : 0
          }
        }
        this.page.total = data.total
        this.dataListLoading = false
        if (done) {
          done()
        }
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.$router.push({
        path: '/prodList/prodInfo',
        query: { prodId: id }
      })
    },
    // 删除和批量删除
    deleteHandle(id) {
      const prodIds = this.getSeleProdIds()
      if (id) {
        prodIds.push(id)
      }
      this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          prodApi.deleteProdInfoById(prodIds).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        })
        .catch(() => { })
    },
    // 条件查询
    searchChange(params, done) {
      this.getDataList(this.page, params, done)
    },
    // 多选变化
    selectionChange(val) {
      this.dataListSelections = val
    },
    // 获取选中的商品Id列表
    getSeleProdIds() {
      return this.dataListSelections.map(item => {
        return item.id
      })
    }
  }
}
</script>
